<template>
  <div class="remote-select">
    <el-select
      style="width: 100%"
      v-model="model"
      filterable
      remote
      default-first-option
      :allow-create="item.allowCreate"
      :multiple="item.multiple"
      :reserve-keyword="item.reserveKeyword || true"
      :disabled="item.disabled"
      :placeholder="getPlaceholder('请输入')"
      :remote-method="remoteMethod"
      :loading="loading"
      :clearable="item.clearable || true"
      @change="handleChange"
    >
      <el-option
        v-for="(it, index) in optionsList"
        :key="index"
        :value="it[item.valueKey || 'id']"
      >
        <div class="option">
          <div class="text">
            <div class="label">{{ it[item.labelKey || "label"] }}</div>
            <div class="extra" v-if="it[item.extraKey || 'extra']">
              {{ it[item.extraKey || 'extra'] }}
            </div>
          </div>
          <div class="remark" v-if="it[item.remarkKey || 'remark']">
            {{ it[item.remarkKey || 'remark'] }}
          </div>
        </div>
      </el-option>
    </el-select>
  </div>
</template>

<script>
import yDefault from "./y-default.vue";
export default {
  name: "remoteSelect",
  includesComponent: ["remoteSelect"],
  extends: yDefault,
  data() {
    return {
      loading: false,
      times: null,
      optionsList: [],
    };
  },
  methods: {
    async queryData() {
      return [
        {
          label: "北京市",
          extra: "东城区",
          remark: "花园小区182号",
        },
        {
          label: "北京市1",
          extra: "东城区1",
          remark: "花园小区182号1",
        },
        {
          label: "北京市2",
          extra: "东城区2",
          remark: "花园小区182号2",
        },
      ];
    },
    async remoteMethod(keyword) {
      this.loading = true;
      clearTimeout(this.times);
      this.times = setTimeout(async () => {
        let list = await this.queryData();
        if (this.item.querySearch) {
          this.optionsList = this.item.querySearch(keyword, list);
        } else {
          this.optionsList = res;
        }
        this.loading = false;
      }, this.item.timeout);
    },
    handleChange() {
      this.$emit("change");
    },
  },
};
</script>

<style lang="scss" scoped>
.option {
  .text {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .label {
      color: #333;
      font-size: 16px;
    }
    .extra {
      color: #8492a6;
      font-size: 13px;
      line-height: 13px;
    }
  }
  .remark {
    color: #8492a6;
    font-size: 13px;
    line-height: 13px;
    padding-bottom: 10px;
  }
}
.el-select-dropdown__item {
  height: auto;
}
</style>
